import router from '@/router'
import { getShareToMyStatus } from '@/api/share'
import { addversionHtml } from '@/api/file'
import { Message, MessageBox } from 'element-ui'

const state = {
  projectId: '',
  projectName: '',
  historyDialogVisible: false,
  authority: {},
  shareToMyStatus: 0,
  selectedFile: { suffix: '' }
}

const mutations = {
  set_projectId(state, projectId) {
    state.projectId = projectId
  },
  set_projectName(state, projectName) {
    state.projectName = projectName
  },
  set_historyDialogVisible(state, type) {
    state.historyDialogVisible = type
  },
  set_authority(state, authority) {
    state.authority = authority
  },
  set_selectedFile(state, selectedFile) {
    state.selectedFile = selectedFile
  }
}

const actions = {
  // user logout
  logout({ commit, state, dispatch }) {
    sessionStorage.userInfo = ''
    sessionStorage.projectId = ''
    router.push(`/login`)
  },
  addversionHtml({ commit, state, dispatch }, { fileid, html }) {
    MessageBox.prompt('', '版本命名', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      closeOnClickModal: false,
      beforeClose: async(action, instance, done) => {
        if (action === 'confirm') {
          console.log(instance)
          instance.confirmButtonLoading = true
          instance.confirmButtonText = '执行中...'
          const name = instance.inputValue
          addversionHtml(fileid, html, name)
            .then(result => {
              Message('保存成功')
              instance.confirmButtonLoading = false
              done()
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          done()
        }
      }
    })
  },
  getShareToMyStatus({ commit, state, dispatch }) {
    getShareToMyStatus()
      .then(result => {
        const { data } = result
        state.shareToMyStatus = data
        console.log(result)
      })
      .catch(err => {
        console.log(err)
      })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
